package com.atfusu.base.impl;

import com.atfusu.base.BaseDao;
import com.atfusu.base.BaseService;
import com.atfusu.util.CastUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * Title:
 * Author: fusu
 * DateTime:  2022/11/25
 * Description:
 **/
public abstract class BaseServiceImpl<T> implements BaseService<T> {

    /**
     * @return com.atfusu.base.BaseDao<T>
     * @Author: fusu
     * @Description: 获取相应dao层对象
     * @DateTime:21:19 2022/11/25
     * @params: []
     */
    protected abstract BaseDao<T> getEntityDao();

    @Override
    public List<T> findAll() {
        return getEntityDao().findAll();

    }

    @Override
    public void insert(T t) {
        getEntityDao().insert(t);
    }

    @Override
    public T getById(Serializable id) {
        return getEntityDao().getById(id);
    }

    @Override
    public void update(T t) {
        getEntityDao().update(t);
    }

    @Override
    public void delete(Serializable id) {
        getEntityDao().delete(id);
    }

    @Override
    public PageInfo<T> findPage(Map<String, Object> filters) {
        //当前页数
        int pageNum = CastUtil.castInt(filters.get("pageNum"), 1);
        //每页多少条数据
        int pageSize = CastUtil.castInt(filters.get("pageSize"), 10);
        //添加分页查询
        PageHelper.startPage(pageNum, pageSize);

        return new PageInfo<T>(getEntityDao().findPage(filters), 5);
    }
}
